﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using LogosoftSharepoint.Data_Sources;
using System.Collections.Generic;

namespace LogosoftSharepoint.WebServices
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class EditTaskService : System.Web.Services.WebService
    {

        /* Utrzymwanie sesji mozna wylaczyc, jesli nie przeszkadza nam, ze zwrocimy
         * dane o zadaniu, osobie niezalogowanej (nieodpowiedniej)
         */
        [WebMethod(EnableSession = true)]
        public TaskAndWorkers GetTaskDetails(int taskID)
        {
            if (Session == null || Session["loggedin"] == null)
            {
                return null;
            }
            /* **** LINQ version **** */
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();

            var task = from t in ssdc.ls_zadania
                       where t.id == taskID
                       select t; // powinno zwrcac jeden element ale mam problem zeby tak zrobic...

            var workers = from wt in ssdc.ls_pracownicy_przy_zadaniach
                        join w in ssdc.ls_pracownicy on wt.id_pracownika equals w.id
                        where wt.id_zadania == taskID
                        select w;

            return new TaskAndWorkers(task.ToList()[0], workers.ToList()); 
        }

        [WebMethod(EnableSession = true)]
        public System.Linq.IQueryable<LogosoftSharepoint.Data_Sources.ls_typy_zadan> GetTaskTypes()
        {
            if (Session == null || Session["loggedin"] == null)
            {
                return null;
            }
            /* **** LINQ version **** */
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();
            var taskTypes = from t
                        in ssdc.ls_typy_zadan
                        select t;

            return taskTypes;
        }

        [WebMethod(EnableSession = true)]
        public System.Linq.IQueryable<LogosoftSharepoint.Data_Sources.ls_pracownicy> GetPeople()
        {
            if (Session == null || Session["loggedin"] == null)
            {
                return null;
            }
            /* **** LINQ version **** */
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();
            var people = from p
                        in ssdc.ls_pracownicy
                        select p;

            return people;
        }

        [WebMethod(EnableSession = true)]
        public System.Linq.IQueryable<LogosoftSharepoint.Data_Sources.ls_poziomy_waznosci> GetUrgencyLevels()
        {
            if (Session == null || Session["loggedin"] == null)
            {
                return null;
            }

            /* **** LINQ version **** */
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();
            var urgencyLvls = from ul
                            in ssdc.ls_poziomy_waznosci
                            select ul;

            return urgencyLvls;
        }

        [WebMethod(EnableSession = true)]
        public System.Linq.IQueryable<LogosoftSharepoint.Data_Sources.ls_projekty> GetProjects()
        {
            if (Session == null || Session["loggedin"] == null)
            {
                return null;
            }

            /* **** LINQ version **** */
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();
            var projects = from p
                            in ssdc.ls_projekty
                            select p;

            return projects;
        }

        [WebMethod(EnableSession = true)]
        public System.Linq.IQueryable<LogosoftSharepoint.Data_Sources.ls_statusy> GetStates()
        {
            if (Session == null || Session["loggedin"] == null)
            {
                return null;
            }

            /* **** LINQ version **** */
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();
            var states = from s
                         in ssdc.ls_statusy
                         select s;

            return states;
        }

        [WebMethod(EnableSession = true)]
        public int InsertNewTask(int projectId,
                                string nazwa, string opis,
                                int status, int typZadania, int poziomWaznosci,
                                List<int> workersId)
        {
            if (Session == null || Session["loggedin"] == null)
            {
                return -1;
            }

            /* **** LINQ version **** */
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();

            Data_Sources.ls_zadania task = new ls_zadania();
            task.projekt = projectId;
            task.nazwa = nazwa;
            task.opis = opis;
            task.status = status;
            task.typ_zadania = typZadania;
            task.poziom_waznosci = poziomWaznosci;
            ssdc.ls_zadania.InsertOnSubmit(task);
            ssdc.SubmitChanges();

            foreach (int workerId in workersId)
            {
                ls_pracownicy_przy_zadaniach workerAtTaskToInsert = new ls_pracownicy_przy_zadaniach();
                workerAtTaskToInsert.id_zadania = task.id;
                workerAtTaskToInsert.id_pracownika = workerId;
                ssdc.ls_pracownicy_przy_zadaniach.InsertOnSubmit(workerAtTaskToInsert);
            }

            ssdc.SubmitChanges();

            return task.id;
        }

        [WebMethod(EnableSession = true)]
        public int ModifyTask(int taskId, int projectId,
                                string nazwa, string opis,
                                int status, int typZadania, int poziomWaznosci,
                                List<int> workersId)
        {
            SharepointSystemDataContext ssdc = new SharepointSystemDataContext();
            try
            {
                // modify all task data despite associated workers
                var task = ssdc.ls_zadania.Single(t => t.id == taskId);
                task.nazwa = nazwa;
                task.opis = opis;
                task.status = status;
                task.typ_zadania = typZadania;
                task.poziom_waznosci = poziomWaznosci;
                task.projekt = projectId;

                // delete workers previously associated with this task
                var workersAtTaskToDelete = ssdc.ls_pracownicy_przy_zadaniach.Where(w => w.id_zadania == taskId);
                ssdc.ls_pracownicy_przy_zadaniach.DeleteAllOnSubmit(workersAtTaskToDelete);

                // add workers who are associated with the task from now on
                foreach(int workerId in workersId){
                    ls_pracownicy_przy_zadaniach workerAtTaskToInsert = new ls_pracownicy_przy_zadaniach();
                    workerAtTaskToInsert.id_zadania = taskId;
                    workerAtTaskToInsert.id_pracownika = workerId;
                    ssdc.ls_pracownicy_przy_zadaniach.InsertOnSubmit(workerAtTaskToInsert);
                }

                ssdc.SubmitChanges();

                return taskId;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        [WebMethod(EnableSession = true)]
        public int SetPeopleForTask(int taskId, List<int> workersId)
        {
            return taskId;
        }

    }
}
